package com.shumm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shumm.common.Result;
import com.shumm.mapper.TraditionalchinesemedicineMapper;
import com.shumm.po.Traditionalchinesemedicine;
import com.shumm.service.TraditionalchinesemedicineService;
import com.shumm.utils.PageUtil;
import com.shumm.vo.TcmAndTcmrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shumm
 * @Description
 * @create 2023-11-17 17:13
 */

@Service
@SuppressWarnings("all")
public class TraditionalchinesemedicineServiceImpl implements TraditionalchinesemedicineService {
    @Autowired
    private TraditionalchinesemedicineMapper mapper;

    @Override
    public Result add(Traditionalchinesemedicine tcm) {
        int i = mapper.addAndGetId(tcm);
        if (i > 0) {
            return Result.success();
        }
        return Result.error("未插入成功");
    }

    @Override
    public Result deleteById(Integer id) {
        if (mapper.deleteById(id) > 0) {
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result deleteByIdWithForeign(Integer id) {
        if (mapper.deleteByIdWithForeign(id) > 0) {
            return Result.success();
        } else {
            if (mapper.deleteById(id) > 0) {
                return Result.success();
            } else {
                return Result.error("删除失败");
            }
        }
    }

    @Override
    public Result update(Traditionalchinesemedicine tcm) {
        if (mapper.isExistedById(tcm.getId()) > 0) {
            mapper.update(tcm);
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result selectById(Integer id) {
        Traditionalchinesemedicine tcm = mapper.selectById(id);
        return Result.success(tcm);
    }

    @Override
    public Result list(int num, int size) {
        PageHelper.startPage(num, size);
        Page<Traditionalchinesemedicine> list = (Page) mapper.selectAll();
        return Result.success(list, new PageUtil(list.getPageNum(), list.getPageSize(), list.getTotal()));
    }

    @Override
    public Result selectByCondition(String name, String alias, String xingwei, String zhuzhi, int num, int size) {
        PageHelper.startPage(num, size);
        Page<Traditionalchinesemedicine> list = (Page) mapper.selectByCondition(name, alias, xingwei, zhuzhi);
        return Result.success(list, new PageUtil(list.getPageNum(), list.getPageSize(), list.getTotal()));
    }

    @Override
    public Result listWithContent(int id) {
        PageHelper.startPage(1, 10);
        Page<TcmAndTcmrVO> tcmAndTcmrVOS = (Page) mapper.selectByIdWithContent(id);
        return Result.success(tcmAndTcmrVOS, new PageUtil(tcmAndTcmrVOS.getPageNum(), tcmAndTcmrVOS.getPageSize(), tcmAndTcmrVOS.getTotal()));
    }

}
